/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.PropertyPriceSource;
import times.persistence.entities.PriceSourceSlaughterHeader;
import times.persistence.entities.SlaughterProperty;

/**
 *
 * @author Administrator
 */
public class SlaughterPropertyJpaController implements Serializable {

    public SlaughterPropertyJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(SlaughterProperty slaughterProperty) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PropertyPriceSource propertyPriceSourceId = slaughterProperty.getPropertyPriceSourceId();
            if (propertyPriceSourceId != null) {
                propertyPriceSourceId = em.getReference(propertyPriceSourceId.getClass(), propertyPriceSourceId.getId());
                slaughterProperty.setPropertyPriceSourceId(propertyPriceSourceId);
            }
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderId = slaughterProperty.getPriceSourceSlaughterHeaderId();
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId = em.getReference(priceSourceSlaughterHeaderId.getClass(), priceSourceSlaughterHeaderId.getId());
                slaughterProperty.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeaderId);
            }
            em.persist(slaughterProperty);
            if (propertyPriceSourceId != null) {
                propertyPriceSourceId.getSlaughterPropertyList().add(slaughterProperty);
                propertyPriceSourceId = em.merge(propertyPriceSourceId);
            }
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId.getSlaughterPropertyList().add(slaughterProperty);
                priceSourceSlaughterHeaderId = em.merge(priceSourceSlaughterHeaderId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(SlaughterProperty slaughterProperty) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SlaughterProperty persistentSlaughterProperty = em.find(SlaughterProperty.class, slaughterProperty.getId());
            PropertyPriceSource propertyPriceSourceIdOld = persistentSlaughterProperty.getPropertyPriceSourceId();
            PropertyPriceSource propertyPriceSourceIdNew = slaughterProperty.getPropertyPriceSourceId();
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderIdOld = persistentSlaughterProperty.getPriceSourceSlaughterHeaderId();
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderIdNew = slaughterProperty.getPriceSourceSlaughterHeaderId();
            if (propertyPriceSourceIdNew != null) {
                propertyPriceSourceIdNew = em.getReference(propertyPriceSourceIdNew.getClass(), propertyPriceSourceIdNew.getId());
                slaughterProperty.setPropertyPriceSourceId(propertyPriceSourceIdNew);
            }
            if (priceSourceSlaughterHeaderIdNew != null) {
                priceSourceSlaughterHeaderIdNew = em.getReference(priceSourceSlaughterHeaderIdNew.getClass(), priceSourceSlaughterHeaderIdNew.getId());
                slaughterProperty.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeaderIdNew);
            }
            slaughterProperty = em.merge(slaughterProperty);
            if (propertyPriceSourceIdOld != null && !propertyPriceSourceIdOld.equals(propertyPriceSourceIdNew)) {
                propertyPriceSourceIdOld.getSlaughterPropertyList().remove(slaughterProperty);
                propertyPriceSourceIdOld = em.merge(propertyPriceSourceIdOld);
            }
            if (propertyPriceSourceIdNew != null && !propertyPriceSourceIdNew.equals(propertyPriceSourceIdOld)) {
                propertyPriceSourceIdNew.getSlaughterPropertyList().add(slaughterProperty);
                propertyPriceSourceIdNew = em.merge(propertyPriceSourceIdNew);
            }
            if (priceSourceSlaughterHeaderIdOld != null && !priceSourceSlaughterHeaderIdOld.equals(priceSourceSlaughterHeaderIdNew)) {
                priceSourceSlaughterHeaderIdOld.getSlaughterPropertyList().remove(slaughterProperty);
                priceSourceSlaughterHeaderIdOld = em.merge(priceSourceSlaughterHeaderIdOld);
            }
            if (priceSourceSlaughterHeaderIdNew != null && !priceSourceSlaughterHeaderIdNew.equals(priceSourceSlaughterHeaderIdOld)) {
                priceSourceSlaughterHeaderIdNew.getSlaughterPropertyList().add(slaughterProperty);
                priceSourceSlaughterHeaderIdNew = em.merge(priceSourceSlaughterHeaderIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = slaughterProperty.getId();
                if (findSlaughterProperty(id) == null) {
                    throw new NonexistentEntityException("The slaughterProperty with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SlaughterProperty slaughterProperty;
            try {
                slaughterProperty = em.getReference(SlaughterProperty.class, id);
                slaughterProperty.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The slaughterProperty with id " + id + " no longer exists.", enfe);
            }
            PropertyPriceSource propertyPriceSourceId = slaughterProperty.getPropertyPriceSourceId();
            if (propertyPriceSourceId != null) {
                propertyPriceSourceId.getSlaughterPropertyList().remove(slaughterProperty);
                propertyPriceSourceId = em.merge(propertyPriceSourceId);
            }
            PriceSourceSlaughterHeader priceSourceSlaughterHeaderId = slaughterProperty.getPriceSourceSlaughterHeaderId();
            if (priceSourceSlaughterHeaderId != null) {
                priceSourceSlaughterHeaderId.getSlaughterPropertyList().remove(slaughterProperty);
                priceSourceSlaughterHeaderId = em.merge(priceSourceSlaughterHeaderId);
            }
            em.remove(slaughterProperty);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SlaughterProperty> findSlaughterPropertyEntities() {
        return findSlaughterPropertyEntities(true, -1, -1);
    }

    public List<SlaughterProperty> findSlaughterPropertyEntities(int maxResults, int firstResult) {
        return findSlaughterPropertyEntities(false, maxResults, firstResult);
    }

    private List<SlaughterProperty> findSlaughterPropertyEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(SlaughterProperty.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public SlaughterProperty findSlaughterProperty(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(SlaughterProperty.class, id);
        } finally {
            em.close();
        }
    }

    public int getSlaughterPropertyCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<SlaughterProperty> rt = cq.from(SlaughterProperty.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
